﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Collections.Generic;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Text;
using System.Net;

using CaseStudy.Web.UserControl;

using ISS.CaseStudy.BLL;
using ISS.CaseStudy.Entities;


namespace CaseStudy.Web
{
    public partial class CaseStudyVIew : System.Web.UI.Page
    {
        private string cnSiteRoot = System.Configuration.ConfigurationManager.AppSettings["CNSiteRoot"].ToString();
        private string caseContentFolder = System.Configuration.ConfigurationManager.AppSettings["CaseContentFolder"].ToString();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if ((Request.QueryString["id"] == null) || 
                    (string.IsNullOrEmpty(Request.QueryString["id"])))
                    return;

                string htmlContent = GetMainHtml();
                mainMiddle.InnerHtml = htmlContent;
                
                lnkMailTo.NavigateUrl = string.Format("mailto:?body=这是一个成功案例的页面，也许你会喜欢： {0}", Request.Url.ToString());
            }
            
        }

        private string GetMainHtml()
        {
            string id = Request.QueryString["id"];
            int result;
            if (!int.TryParse(id, out result))
                return "";

            CaseStudyInfo caseStudyInfo = CaseStudyBLL.GetInfo(id);
            if (caseStudyInfo == null)
                return "";

            string requestUrl = "http://" + Request.Url.Authority + cnSiteRoot + "/" + caseContentFolder + "/" + caseStudyInfo.FileName;

            string pageContent = GetPageContent(requestUrl, Encoding.Default);

            int beingIndex = pageContent.IndexOf("<body>");

            if (beingIndex < 0)
                return "";

            int endIndex = pageContent.IndexOf("</body>");
            int length = endIndex - beingIndex - 6;

            string content = pageContent.Substring(beingIndex + 6, length);
            return content;
        }

        private string GetPageContent(string Url, Encoding encode)
        {
            try
            {
                HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(Url);
                HttpWebResponse wResp = wReq.GetResponse() as HttpWebResponse;
                System.IO.Stream respStream = wResp.GetResponseStream();

                System.IO.StreamReader reader = new System.IO.StreamReader(respStream, encode);
                string content = reader.ReadToEnd();

                reader.Close();
                reader.Dispose();
                return content;
            }
            catch (System.Exception ex)
            {
                return ex.Message;
            }
        }
    }
}